package com.jack.jetpackapp.expand

import android.app.Activity
import android.view.View

object ClickBinder {

    fun init(activity: Activity) {
        val clazz = activity.javaClass
        clazz.declaredMethods.forEach { method ->
            val params = method.genericParameterTypes
            val bindClick = method.getAnnotation(BindClick::class.java)
            bindClick?.apply {
                if (params.size == 1) {
                    // 打开访问权限
                    method.isAccessible = true
                    ids.forEach { id ->
                        val view = activity.findViewById<View>(id)
                        view?.setOnClickListener {
                            method.invoke(activity, it)
                        }
                    }
                    // 关闭访问权限
                    method.isAccessible = false
                    return@forEach
                } else {
                    throw RuntimeException("${method.name}只能有一个参数")
                }
            }
        }
    }
}